namespace BM.Tools.WebControls.MForm.Navigation
{
    using System.Collections.Generic;

    using BM.Tools.WebControls.MForm.Navigation.NodeTests;
    using BM.Tools.WebControls.MForm.Navigation.Predicates;
    using BM.Tools.WebControls.MForm.Navigation.XPath;

    /// <summary>
    /// A navigator for the <see cref="SingleLocationStep" /> locator.
    /// </summary>
    /// <typeparam name="T">
    /// The type of the navigable object.
    /// </typeparam>
    public class SingleLocationStepNavigator<T> : LocationStepNavigator<T, SingleLocationStep>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SingleLocationStepNavigator{T}"/> class. 
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="locationStep">
        /// The location step.
        /// </param>
        public SingleLocationStepNavigator(INavigator<T> parent, SingleLocationStep locationStep)
                : base(parent, locationStep)
        {
        }

        /// <summary>
        /// Selects those navigables that were found by the node test navigator and filters them with the predicate navigator.
        /// </summary>
        /// <returns>
        /// the enumerable of navigables
        /// </returns>
        public override IEnumerable<T> Select()
        {
            var navigator = NodeTestNavigatorFactory<T>.Create(Parent, Locator.NodeTest);
            if (Locator.Predicate == null)
            {
                return navigator.Select();
            }

            var predicateNavigator = PredicateNavigatorFactory<T>.Create(navigator, Locator.Predicate);
            return predicateNavigator.Select();
        }
    }
}